package com.laowang.project.system.service.impl;

import java.util.List;

import com.laowang.common.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.laowang.project.system.mapper.MyCyhPromoPartnerMapper;
import com.laowang.project.system.domain.MyCyhPromoPartner;
import com.laowang.project.system.service.MyCyhPromoPartnerService;

/**
 * 推广大使合伙人配置Service业务层处理
 *
 * @author laowang
 * @date 2024-01-12
 */
@Service
public class MyCyhPromoPartnerServiceImpl implements MyCyhPromoPartnerService {
    @Autowired
    private MyCyhPromoPartnerMapper myCyhPromoPartnerMapper;

    /**
     * 查询推广大使合伙人配置
     *
     * @param id 推广大使合伙人配置ID
     * @return 推广大使合伙人配置
     */
    @Override
    public MyCyhPromoPartner selectMyCyhPromoPartnerById(Integer id) {
        return myCyhPromoPartnerMapper.selectMyCyhPromoPartnerById(id);
    }

    /**
     * 查询推广大使合伙人配置列表
     *
     * @param myCyhPromoPartner 推广大使合伙人配置
     * @return 推广大使合伙人配置
     */
    @Override
    public List<MyCyhPromoPartner> selectMyCyhPromoPartnerList(MyCyhPromoPartner myCyhPromoPartner) {
        return myCyhPromoPartnerMapper.selectMyCyhPromoPartnerList(myCyhPromoPartner);
    }

    /**
     * 新增推广大使合伙人配置
     *
     * @param myCyhPromoPartner 推广大使合伙人配置
     * @return 结果
     */
    @Override
    public int insertMyCyhPromoPartner(MyCyhPromoPartner myCyhPromoPartner) {
        Integer promoteId = myCyhPromoPartner.getPromoteId();
        Integer partnerId = myCyhPromoPartner.getPartnerId();
        if (promoteId == null){
            throw new CustomException("推广大使不能为空");
        }
        if (partnerId == null){
            throw new CustomException("合伙人不能为空");
        }

        MyCyhPromoPartner myCyhPromoPartner1  =   myCyhPromoPartnerMapper.selectOneByProAndPar(promoteId,partnerId);
        if (myCyhPromoPartner1 != null){
            throw new CustomException("该推广大使与合伙人的配置已存在");
        }
        return myCyhPromoPartnerMapper.insertMyCyhPromoPartner(myCyhPromoPartner);
    }

    /**
     * 修改推广大使合伙人配置
     *
     * @param myCyhPromoPartner 推广大使合伙人配置
     * @return 结果
     */
    @Override
    public int updateMyCyhPromoPartner(MyCyhPromoPartner myCyhPromoPartner) {
        Integer promoteId = myCyhPromoPartner.getPromoteId();
        Integer partnerId = myCyhPromoPartner.getPartnerId();
        if (promoteId == null){
            throw new CustomException("推广大使不能为空");
        }
        if (partnerId == null){
            throw new CustomException("合伙人不能为空");
        }

        MyCyhPromoPartner myCyhPromoPartner1  =   myCyhPromoPartnerMapper.selectOneByProAndPar(promoteId,partnerId);
        if (myCyhPromoPartner1 !=null){
            if (!myCyhPromoPartner1.getId().equals(myCyhPromoPartner.getId())){
                throw new CustomException("该推广大使与合伙人的配置已存在");
            }
        }


        return myCyhPromoPartnerMapper.updateMyCyhPromoPartner(myCyhPromoPartner);
    }

    /**
     * 批量删除推广大使合伙人配置
     *
     * @param ids 需要删除的推广大使合伙人配置ID
     * @return 结果
     */
    @Override
    public int deleteMyCyhPromoPartnerByIds(Integer[] ids) {
        return myCyhPromoPartnerMapper.deleteMyCyhPromoPartnerByIds(ids);
    }

    /**
     * 删除推广大使合伙人配置信息
     *
     * @param id 推广大使合伙人配置ID
     * @return 结果
     */
    @Override
    public int deleteMyCyhPromoPartnerById(Integer id) {
        return myCyhPromoPartnerMapper.deleteMyCyhPromoPartnerById(id);
    }
}
